import { NzTreeNodeOptions } from 'ng-zorro-antd/tree';

export class TaskParam {
  [key: string]: any;
  id!: number;
  name!: string;
  target: string | undefined;
  tags: string | undefined;
  description: string | undefined;
  processStatus: number | undefined;
  classify: number | undefined;
  importance: number | undefined;
  end!: string;
  periodEndTime?: string;
}

export class PhasedTaskParam extends TaskParam {
  [key: string]: any;
  begin!: string;
  parentId: number | undefined;
  level: number | undefined;
}

export class PeriodicTaskParam extends TaskParam {
  repeatMode: RepeatMode = 0;
  repeatNum = 0;
  periodEndTime?: string | undefined;
  prevStatus?: number;
  currStatus?: number;
}

export class PhasedTaskWithChildrenParam extends PhasedTaskParam {
  [key: string]: any;
  children?: Array<PhasedTaskWithChildrenParam>;
}

export class Task {
  id!: number;
  name!: string;
  target: string | undefined;
  tags: string | undefined;
  description: string | undefined;
  periodEndTime?: Date;
  /*
    * 工作进程状态
     * 0 '未完成'
     * 1 "已完成"
     * 2 "进行中"
  */
  processStatus: number | undefined;
  classify: number | undefined;
  importance: number | undefined;
  end: Date | undefined;
  constructor(param: TaskParam) {
    this.id = param.id;
    this.name = param.name;
    this.target = param.target;
    this.tags = param.tags;
    this.description = param.description;
    this.processStatus = param.processStatus;
    this.classify = param.classify;
    this.importance = param.importance;
    this.end = new Date(param.end);
    this.periodEndTime = param.periodEndTime ? new Date(param.periodEndTime) : undefined;
  }
}

export class PhasedTask extends Task {
  begin: Date | undefined;
  parentId: number | undefined;
  level: number | undefined;
  constructor(param: PhasedTaskParam) {
    super(param);
    this.begin = new Date(param.begin);
    this.parentId = param.parentId;
    this.level = param.level;
  }
}

export class PeriodicTask extends Task {
  repeatMode: RepeatMode;
  repeatNum;
  periodEndTime?: Date;
  prevStatus?: number;
  currStatus?: number;
  constructor(param: PeriodicTaskParam) {
    super(param);
    this.repeatMode = param.repeatMode;
    this.repeatNum = param.repeatNum;
    this.periodEndTime = param.periodEndTime ? new Date(param.periodEndTime) : undefined;
    this.prevStatus = param.prevStatus;
    this.currStatus = param.currStatus;
  }
}
export class PhasedTaskWithChildren extends PhasedTask {
  children?: Array<PhasedTaskWithChildren>;
  constructor(param: PhasedTaskWithChildrenParam) {
    super(param);
    this.children = new Array();
    if (param.children && param.children.length > 0) {
      for (const item of param.children) {
        this.children.push(new PhasedTaskWithChildren(item));
      }
    }
  }
  public convertToNzTreeNodeOption(): NzTreeNodeOptions {
    const node: NzTreeNodeOptions = {
      title: this.name,
      key: this.id.toPrecision(),
    };
    if (this.children && this.children.length > 0) {
      node.isLeaf = false;
      node.children = [];
      for (const child of this.children) {
        node.children.push(child.convertToNzTreeNodeOption());
      }
    } else {
      node.isLeaf = true;
    }
    return node;
  }
}

export enum RepeatMode {
  preDay = 0,
  preWeek = 1,
  preMonth = 2,
  preQuarter = 3,
  preHalfYear = 4,
  preYear = 5,
}

export enum ProcessStatus {
  undone = 0,
  done = 1,
  processing = 2,
}

export enum TaskClassfiy {
  phase= 0,
  period = 1,
}
